// 找不同页点击
var that = this
export default class Game1Click {
	constructor() {
		that = this
		this.failsum = GameGlobal.databus.failsum
		this.goldNumber = GameGlobal.databus.goldNumber
		this.gamedb = {
			gameStatus: 'init', //init初始化  success成功 fail失败
			failTotal: 0, //失败总数
		}
		let canvas = GameGlobal.databus.canvas
		let btn1Bg = GameGlobal.databus.game1image.btn1Bg //失败按钮1
		let btn2Bg = GameGlobal.databus.game1image.btn2Bg //失败按钮2
		let btn3Bg = GameGlobal.databus.game1image.btn3Bg //成功按钮1
		let btn4Bg = GameGlobal.databus.game1image.btn4Bg //成功按钮2
		//失败按钮1
		this.tempbtn1 = {
			x: canvas.width / 2 - btn1Bg.box.w / 2 + btn1Bg.box.x,
			y: canvas.height / 2 - btn1Bg.box.h / 2 + btn1Bg.box.y,
			w: btn1Bg.box.w,
			h: btn1Bg.box.h,
		}
		//失败按钮2
		this.tempbtn2 = {
			x: canvas.width / 2 - btn2Bg.box.w / 2 + btn2Bg.box.x,
			y: canvas.height / 2 - btn2Bg.box.h / 2 + btn2Bg.box.y,
			w: btn2Bg.box.w,
			h: btn2Bg.box.h,
		}
		//成功按钮1
		this.tempbtn3 = {
			x: canvas.width / 2 - btn3Bg.box.w / 2 + btn3Bg.box.x,
			y: canvas.height / 2 - btn3Bg.box.h / 2 + btn3Bg.box.y,
			w: btn3Bg.box.w,
			h: btn3Bg.box.h,
		}
		//成功按钮2
		this.tempbtn4 = {
			x: canvas.width / 2 - btn4Bg.box.w / 2 + btn4Bg.box.x,
			y: canvas.height / 2 - btn4Bg.box.h / 2 + btn4Bg.box.y,
			w: btn4Bg.box.w,
			h: btn4Bg.box.h,
		}
	}
	// 点击事件
	handleTouch(e) {
		const touch = e.touches[0];
		const x = touch.clientX;
		const y = touch.clientY;
		that.game1Click(x, y);
	}
	// 找不同游戏页面点击
	game1Click(x, y) {
		let gameSpots = GameGlobal.databus.gameSpots
		if (this.gamedb.gameStatus === 'init') {
			// 点击圆圈
			const hasAny = gameSpots.find(spot => {
				return this.isInRect(x, y, spot);
			});
			if (hasAny) {
				GameGlobal.musicManager.playSuccessMusic()
				this.cikyuan(hasAny) //点击圆圈
			} else {
				GameGlobal.musicManager.playFailCikMusic()
				this.cikkongbai() //点击空白位置
			}
		} else if (this.gamedb.gameStatus === 'fail') {
			// 点击失败弹窗按钮
			if (this.isInRect(x, y, this.tempbtn1)) {
				this.cikfailbtn1() //失败点击退出
			} else if (this.isInRect(x, y, this.tempbtn2)) {
				this.cikfailbtn2() //失败点击继续
			}
		} else if (this.gamedb.gameStatus === 'success') {
			// 点击重来弹窗按钮
			if (this.isInRect(x, y, this.tempbtn3)) {
				this.ciksuccessbtn1() //成功点击重来

			} else if (this.isInRect(x, y, this.tempbtn4)) {
				this.ciksuccessbtn2() //成功点击返回
			}
		}
	}
	// 点击圆圈
	cikyuan(hasAny) {
		let ctx = GameGlobal.databus.ctx
		let gameSpots = GameGlobal.databus.gameSpots
		hasAny.isClicked = true;
		// 画水墨圆
		this.cavasyuan((hasAny.x + hasAny.w / 2), (hasAny.y + hasAny.h / 2), (hasAny.h / 2 + 10), () => {
			const totalClicked = gameSpots.filter(item => item.isClicked === true).length;
			if (totalClicked >= 5) {
				this.gamedb.gameStatus = 'success'
				const Popok = require('../common/popok.js')
				Popok.default.render(ctx)
			}
		})
	}
	//点击空白位置
	cikkongbai() {
		let ctx = GameGlobal.databus.ctx
		this.gamedb.failTotal += 1
		if (this.gamedb.failTotal >= this.failsum) {
			this.gamedb.gameStatus = 'fail'
			const Popfail = require('../common/popfail.js')
			Popfail.default.render(ctx)
		}
	}
	// 失败点击退出
	cikfailbtn1() {
		this.gamedb.failTotal = 0
		this.gamedb.gameStatus = 'init'
		GameGlobal.databus.gameSpots = []
		GameGlobal.databus.gamethis.notrender()
		GameGlobal.databus.homethis.render()
		console.log("点击退出游戏")
	}
	// 失败点击继续
	cikfailbtn2() {
		if (GameGlobal.databus.goldNumber >= 100) {
			GameGlobal.databus.goldNumber -= 100
			that.gamedb.failTotal = 0
			that.gamedb.gameStatus = 'init'
			// gameSpots.forEach(spot => spot.isClicked = false);
			GameGlobal.databus.gamethis.update()
		} else {
			this.jlvideoAd() // 激励广告
		}
	}
	// 成功点击重来
	ciksuccessbtn1() {
		let gameSpots = GameGlobal.databus.gameSpots
		this.gamedb.failTotal = 0
		this.gamedb.gameStatus = 'init'
		gameSpots.forEach(spot => spot.isClicked = false);
		GameGlobal.databus.gamethis.update()
		console.log("成功点击重来")
	}
	// 成功点击返回
	ciksuccessbtn2() {
		console.log("点击返回");
		this.gamedb.failTotal = 0
		this.gamedb.gameStatus = 'init'
		GameGlobal.databus.gameSpots = []
		GameGlobal.databus.gamethis.notrender()
		GameGlobal.databus.homethis.render()
	}

	// 激励广告
	jlvideoAd() {
		const that = this
		GameGlobal.databus.videoAd.show().then(() => {})

		function listener(res) {
			GameGlobal.databus.videoAd.offClose(listener);
			console.log('取消观看视频', res.isEnded)
			if (res.isEnded) {
				that.gamedb.failTotal = 0
				that.gamedb.gameStatus = 'init'
				// gameSpots.forEach(spot => spot.isClicked = false);
				GameGlobal.databus.gamethis.update()
			} else {}
			if (res.count) {
				console.log('观看视频数量：', res.count)
			}
		}
		GameGlobal.databus.videoAd.onClose(listener); //这里监听广告的close 事件
	}
	// 水墨圆
	cavasyuan(x, y, r, func) {
		let ctx = GameGlobal.databus.ctx
		let progress = 0;
		const center = { x: x, y: y };
		const radius = r;

		function draw() {
			// 创建水墨渐变
			const gradient = ctx.createRadialGradient(
				center.x, center.y, radius * 0.6,
				center.x, center.y, radius
			);
			gradient.addColorStop(0, 'rgba(200,0,0,0.9)');
			gradient.addColorStop(1, 'rgba(200,0,0,0.2)');
			// 绘制动画笔触
			ctx.beginPath();
			ctx.arc(center.x, center.y, radius, -Math.PI / 2, -Math.PI / 2 + progress * Math.PI * 2);
			ctx.lineWidth = 3;
			ctx.strokeStyle = 'rgba(200,0,0,0.9)';
			ctx.lineCap = 'round';

			// 添加毛边效果
			ctx.shadowColor = 'rgba(200,0,0,0.5)';
			ctx.shadowBlur = 3;
			ctx.stroke();
			// 更新进度（缓动动画）
			progress += (1 - progress) * 0.04;
			if (progress < 0.90) {
				if (GameGlobal.databus.gameState === 'game1') {
					requestAnimationFrame(draw);
				}
			} else {
				func && func()
			}
		}
		requestAnimationFrame(draw);
	}
	// 辅助函数
	isInRect(x, y, rect) {
		return x >= rect.x && x <= rect.x + rect.w &&
			y >= rect.y && y <= rect.y + rect.h;
	}

	isInCircle(x, y, circle) {
		const dx = x - circle.x;
		const dy = y - circle.y;
		return dx * dx + dy * dy <= circle.r * circle.r;
	}

}